package com.zhxy.manager.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhxy.manager.dao.StudentScoreDao;
import com.zhxy.manager.dto.StudentScoreDto;
import com.zhxy.manager.dto.StudentScoreTjDto;
import com.zhxy.manager.entity.StudentScore;
import com.zhxy.manager.excel.SongExcel;
import com.zhxy.manager.excel.StudentScoreExcel;
import com.zhxy.manager.service.intf.StudentScoreService;
import com.zhxy.manager.vo.PageBean;
import com.zhxy.manager.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentScoreImpl implements StudentScoreService {
   @Autowired
   private StudentScoreDao dao;

    @Override
    public R save(StudentScore studentScore) {
       if (dao.add(studentScore)>0){
           return R.ok();
       }else {
           return R.fail();
       }
    }

    @Override
    public R update(StudentScore studentScore) {
       if (dao.update(studentScore)>0){
           return R.ok();
       }else {
           return R.fail();
       }
    }

    @Override
    public R del(int id) {
       if (dao.del(id)>0){
           return R.ok();
       }else {
           return R.fail();
       }
    }

    @Override
    public PageBean queryPage(int limit, int page) {
        PageHelper.startPage(limit, page);
        PageInfo<StudentScore> pageInfo=new PageInfo<>();
        return PageBean.bean(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public List<StudentScore> selectAll() {
        return dao.selectAll();
    }

    @Override
    //  返回值类型是个对象   方法名（形参）
    public PageBean queryPageWhere(String name, int sex, int limit, int page) {

            if(!StringUtils.hasLength(name)){
                name=null;
            }else{
                name="%"+name+"%";//包含  模糊查询
            }
            PageHelper.startPage(page,limit);
            //泛型类
           PageInfo<StudentScore> pageInfo=new PageInfo<>(dao.selectWhere(name,sex));
        return PageBean.bean(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public R delBatch(int[] ids) {
        //1.验证参数
        if(ids!=null && ids.length>0){
            //2.执行
            if(dao.delBatch(ids)>0){
                return R.ok();
            }
        }
        return R.fail();
    }



    @Override
    public PageBean queryDetail(String course, int page, int limit) {
        //1.条件处理
        if(StringUtils.hasLength(course)){
            course="%"+course+"%";
        }else{
            course=null;
        }
        //2.分页
        PageHelper.startPage(page, limit);
        //3.获取分页结果
        PageInfo<StudentScoreDto> pageInfo=new PageInfo<>(dao.selectDetail(course));
        //4.返回分页的数据
        return PageBean.bean(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R queryTj() {
         //1.查询统计数据
        List<StudentScoreTjDto> list = dao.selectTj();
        //2.根据前端组装数据
        Map<String,List<Object>> map= new HashMap<>();
        map.put("course",new ArrayList<>());//课程
        map.put("score",new ArrayList<>());//成绩
        //3.组装数据  函数时编程
        list.forEach(f->{
            map.get("course").add(f.getCourse());
            map.get("score").add(f.getScore());
        });
        return R.ok(map);
    }

    @Override
    //上传问题  MultipartFile  只要不为空就型穿
    public R saveBatch(MultipartFile file) throws IOException {
        if(!file.isEmpty()){
            //监听器new Rea
            List<StudentScoreExcel> list= EasyExcel.read(file.getInputStream(), StudentScoreExcel.class, new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                }
            }).sheet().doReadSync();
            //执行dao 批量新增
            if(dao.addBatch(list)>0){
                return R.ok();
            }
        }
        return R.fail();
    }
}
